<?php
class NICED_HttpBase
{
    protected $handler;
    
    public function __construct()
    {

    }

    /**
     * @param array $headers
     * @return bool
     * @access privte
     *
     */
    public function setHeaders(Array $headers)
    {
        foreach ($headers as $k => $v)
        {
            unset($headers[$k]);
            $k = self::formatHeader($k);
            $headers[$k] = $v;
        }

        $this->headers = $headers;
        
        return true;
    }

    /**
     * @return array 
     * @access public 
     *
     */
    public function getHeaders()
    {
        return $this->headers;
    }

    public function getHeader($k)
    {
        $k = self::formatHeader($k);
        $headers = &$this->getHeaders();

        return isset($headers[$k]) ? $headers[$k] : NULL;
    }

    public function setHeader($k, $v)
    {
        $headers = &$this->getHeaders();
        $k = self::formatHeader($k);
        $headers[$k] = $v;
        $this->setHeaders($headers);
    }

    protected static function formatHeader(&$k)
    {
        $k = str_replace("-", "_", strtoupper($k));
        return $k;
    }

    public function setContentType($v)
    {
        return $this->setHeader("Content_Type", $v);
    }

    public function getContentType()
    {
        return $this->getHeader("content_type");
    }

    /**
     * @return array 
     * @access public 
     * @static
     *
     */
    public static function loadHeaders()
    {
        $headers = array();
        foreach ($_SERVER as $k => $v)
        {
            if ("HTTP_" == substr($k, 0, 5))
            {
                $k = substr($k, 5);
                self::formatHeader($k);
                $headers[$k] = $v; 
            }
        }

        return $headers;
    }

    public function initHandler()
    {
        $this->setHandler(new NICED_ExceptionHandler());

        return true;
    }


    public function getExceptions()
    {
        return $this->getHandler()->getExceptions();
    }

    /**
     * @return bool
     * @access public
     *
     */
    public function handle(Exception $e)
    {
        $this->getHandler()->handle($e);

        return true;
    }

    /**
     * @return array 
     * @access public
     *
     */
    public function getHandler()
    {
        return $this->handler;
    }

    /**
     * @param  ExceptionHandler $handler
     * @return bool 
     * @access public
     */
    public function setHandler($handler)
    {
        $this->handler = $handler;

        return true;
    }

    public function __destruct()
    {
        $hander = $this->getHandler();
        unset($handler);
    }
}//end class
